package com.garrett.demo.sys.service;

import com.garrett.demo.sys.dao.UserDao;
import com.garrett.demo.sys.dao.UserDaoTextImp;
import com.garrett.demo.sys.dao.UserDataBaseDemo;
import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;
import com.garrett.demo.sys.test.ApplicationTest;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImp extends AbstractAccountService {

    /**
     * 验证信息
     *
     * @param account  账户名
     * @param password 密码
     * @return 用户信息
     */
    public SimsStudent checkLogin(String account, String password) {

        UserDataBaseDemo userDao = new UserDataBaseDemo();
        Account ac = userDao.getUserByAccount(account);
        UserDataBaseDemo x=ApplicationTest.getUserDataBaseDemo();
        // 这里注意我们要用到空值判断，后期我们会优化掉这一部分
        for (int i = 0; i < x.getAccounts().length; i++) {
            if(x.getAccounts()[i] != null&&x.getAccounts()[i].getAccount().equals(account)) {
                if (null != ac && ac.getPassword().equals(x.getAccounts()[i].getPassword())) {
                    return userDao.getStudentById(ac.getId());
                }
            }
        }
        return null;
    }

    @Override
    public boolean verifyPassword(String pw1,String pw2){
        if(pw1.equals(pw2)){
            return true;
        }
        return false;
    }
    public boolean verifyPassword1(String pw1){
        if(!(pw1.length()>=6&&pw1.length()<=16)){
            return true;
        }
        return false;
    }
    @Override
    public boolean verifyIdCard(String idCard) {
        if(idCard.length()!=18){
            return true;
        }
        return false;
    }
    public boolean verifyMobilePhone(String mobilePhone) {
        if(mobilePhone.startsWith("(+86)")){
            if(mobilePhone.length()==16){
                for (int i = 5; true; i++) {
                    return mobilePhone.charAt(i) < '0' || mobilePhone.charAt(i) > '9';
                }
            }
        } else if(mobilePhone.length()==11) {
            for (int i = 0; true; i++) {
                return mobilePhone.charAt(i) < '0' || mobilePhone.charAt(i) > '9';
            }
        }else {
            return true;
        }
        return true;
    }

    @Override
    public boolean verifyStuId(String id) {
        if(id.length()!=9){
            return true;
        }
        return false;
    }
    public int verifyStuId1(String id){
         UserDaoTextImp s=new UserDaoTextImp();
        for (int i = 0; i <s.count(); i++) {
                if(s.getStudent()[i].getStudentId().equals(id)){
                    return 0;
            }
        }
        return 1;
    }
    public int verifyGender(String gender) {
       return Integer.parseInt(gender);
    }
    @Override
    public void createAccount(Object... obj) {
        Account ac = (Account)obj[0];
        SimsStudent stu = (SimsStudent) obj[1];
        UserDao userDao = new UserDataBaseDemo();
        userDao.addAccount(ac);
        userDao.createUser(stu);
    }

    @Override
    public Account[] getAllAccounts() {
        return new Account[0];
    }

    @Override
    public SimsStudent[] getAllStudents() {
        return new SimsStudent[0];
    }

    @Override
    public SimsStudent getStudentById(String studentId) {
        return null;
    }

    @Override
    public void modify(Object... objs) {

    }

    @Override
    public void deleteAccount(String id) {

    }
}
